package com.erp.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName DateUtils
 * @Description 测试用例类
 * @Author songjizhen
 * @Date 2020/6/17 14:02
 * @Version 1.0
 */
public class DateUtils {

    /** 分 */
    public static final long MINUTE_TTL = 60 * 1000l;
    /** 时 */
    public static final long HOURS_TTL = 60 * 60 * 1000l;
    /** 半天 */
    public static final long HALF_DAY_TTL = 12 * 60 * 60 * 1000l;
    /** 天 */
    public static final long DAY_TTL = 24 * 60 * 60 * 1000l;
    /** 月 */
    public static final long MONTH_TTL = 30 * 24 * 60 * 60 * 1000l;
    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     *
     * @return
     */
    public static Long getSystemTimeSeconds() {
        return System.currentTimeMillis() / 1000L;
    }

    /**
     * 将时间戳转换为时间
     * 输入形如：1488510674 输出形如：2017.03.03 HH:mm:ss
     * 异常格式返回-1
     *
     * @param s
     * @return
     */
    public static String getTimestampDateTimeLong(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

        Date date = new Date(s * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间
     * 输入形如：1488510674 输出形如：2017.03.03
     * 异常格式返回-1
     *
     * @param s
     * @return 
     */
    public static String getTimestampDateLong(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");

        Date date = new Date(s * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     * @param date  日期
     * @return  返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化
     * @param date  日期
     * @param pattern  格式，如：DateUtils.DATE_TIME_PATTERN
     * @return  返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 日期类型转换(将String类型转化为Date类型)
     * @param date String类型的日期
     * @param pattern 日期格式
     * @return 返回Date类型日期
     * @throws ParseException
     */
    public static Date convertStringToDate(String date, String pattern) throws ParseException {
        if(StringUtils.isNotEmpty(date)){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.parse(date);
        }
        return null;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past 过去天数
     * @return
     */
    public static String getPastDateByDay(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        String result = format.format(today);
        return result;
    }
    /**
     * 获取未来第几天的日期
     *
     * @param past 未来天数
     * @return
     */
    public static Date getNextDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        /*SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        String result = format.format(today);*/
        return today;
    }

    /**
     * 获取过去第几年的日期
     * @param year 过去几年
     * @return
     */
    public static String getPastDateByYear(int year) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -year);
        Date today = calendar.getTime();
        String result = format.format(today);
        return result;
    }

    /**
     * 获取指定时间段内相距的天数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 相距天数
     * @throws ParseException
     */
    public static int daysBetween(Date startTime, Date endTime) throws ParseException {
        if(null == startTime){
            startTime = new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
        startTime = sdf.parse(sdf.format(startTime));
        endTime = sdf.parse(sdf.format(endTime));
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endTime);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    // 获得某天最小时间 2020-12-22 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获得某天最大时间 2020-12-22 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Map<String,Date> getNowMonth(){
        Map<String,Date> map = new LinkedHashMap<String, Date>();
        String count_date="2020-02-05";
        //获取月初
        LocalDate monthOfFirstDate=LocalDate.parse(count_date, DateTimeFormatter.ofPattern("yyyy-MM-dd")).with(TemporalAdjusters.firstDayOfMonth());
        //获取月末
        LocalDate monthOfLastDate=LocalDate.parse(count_date, DateTimeFormatter.ofPattern("yyyy-MM-dd")).with(TemporalAdjusters.lastDayOfMonth());
        TemporalAdjusters.firstDayOfMonth();

        System.out.println(monthOfFirstDate);
        System.out.println(monthOfLastDate);
        return map;
    }

    public static Map<String,Date> getNowMonthNew(){
        Map<String,Date> map = new LinkedHashMap<String, Date>();
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime startDate = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime endDate = date.with(TemporalAdjusters.lastDayOfMonth());
//        System.out.println("startDate:" + startDate);
//        System.out.println("endDate:" + endDate);
        map.put("startDate",Date.from(startDate.atZone( ZoneId.systemDefault()).toInstant()));
        map.put("endDate",Date.from(endDate.atZone( ZoneId.systemDefault()).toInstant()));
        return map;
    }

    /**
     * 获取当月结束年月日
     * @return 当月结束年月日
     */
    public static Date getEndTimeOfTheMonth(){
        Calendar calendar = Calendar.getInstance();
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar cal = (Calendar)calendar.clone();
        cal.add(Calendar.DATE, maxDay-day);
        Date startDate = cal.getTime();

        return startDate;
    }

    /**
     * 获取当月开始年月日
     * @return 当月开始年月日
     */
    public static Date getStartTimeOfTheMonth(){
        Calendar calendar = Calendar.getInstance();
        int minDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar cal = (Calendar)calendar.clone();
        cal.add(Calendar.DATE, minDay-day);
        Date endDate = cal.getTime();

        return endDate;
    }

    /**
     * 给指定时间加上天数
     * @param num 天数
     * @param newDate 指定日期
     * @return 加上天数后的时间
     * @throws ParseException
     */
    public static String plusDay(int num, Date newDate, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar ca = Calendar.getInstance();
        ca.setTime(newDate);
        ca.add(Calendar.DATE, num);
        Date currDate = ca.getTime();
        String endDate = format.format(currDate);
        return endDate;
    }

    /**
     * 给指定时间加上月份
     * @param payDate 指定时间
     * @param n 月数
     * @return
     */
    public static String addMonth(Date payDate, int n, String pattern){
        Calendar calendar = Calendar.getInstance();
        //开始时间
        calendar.setTime(payDate);
        calendar.add(Calendar.MONTH, n);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String resDate = sdf.format(calendar.getTime());
        return resDate;
    }

}
